// const { ethers } = require("hardhat");

async function main() {
  // 1. 获取合约实例的地址
  const greeterAddress = "0x5CC307268a1393AB9A764A20DACE848AB8275c46"; // 替换为你实际部署后的合约地址
  
  // 2. 获取当前签名账户
  const [owner] = await ethers.getSigners();
  console.log("Deploying account:", owner.address);

  // 3. 获取 Greeter 合约工厂
  const Greeter = await ethers.getContractFactory("Greeter");

  // 4. 获取已部署合约的实例
  const greeter = Greeter.attach(greeterAddress);
  console.log("Greeter contract address:", greeter.address);

  // 5. 调用 `greet()` 方法
  const greeting = await greeter.greet();
  console.log("Current greeting:", greeting);

  // 6. 调用 `setGreeting()` 方法更新问候语
  const newGreeting = "Hello, Hardhat with ethers!";
  const tx = await greeter.setGreeting(newGreeting);
  await tx.wait(); // 等待交易确认
  console.log("Greeting updated!");

  // 7. 调用 `greet()` 方法确认更新
  const updatedGreeting = await greeter.greet();
  console.log("Updated greeting:", updatedGreeting);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
